/**
 * Created by YXG-PC on 2014/7/22.
 */

var CanvasDown = function(){

    this.parent = {};

    this.width  = 0;

    this.height = 0;

    this.children = [];

    this.appendCanvas = {};

    this.button = {};

};

CanvasDown.prototype = {

    init : function(){

        this.width  = this.parent.offsetWidth;

        this.height = this.parent.offsetHeight;

    },

    saveCanvas : function(){

        //获取parent 下面的所有图层
        this.children = this.parent.getElementsByTagName("canvas");

        if( this.children.length > 0){

            var oCan = this.children[0];

            this.width = oCan.offsetWidth;

            this.height = oCan.offsetHeight;

        }

        //将所有图层转换成一张图片

        this.changeToImage( this.children );




    },

    createLogoCanvas : function( ctx ,fnEnd){

        var img = new Image();

        var _this = this;

        //水印图片
        img.src = "";

        img.onload = function (){

            var pattern = ctx.createPattern(img, "repeat");

            ctx.fillStyle = pattern;

            ctx.beginPath();

            ctx.rect(0, 0, _this.width ,_this.height );

            ctx.fill();

            ctx.closePath();

            ctx.restore();

            if(fnEnd) fnEnd();
        }

    },
    /**
     * 转换为图片
     **/
    changeToImage:function( canvases ){

        var _this = this;

        var can = document.createElement("canvas");

        can.width = this.width;

        can.height = this.height;

        var ctx = can.getContext("2d");

        //加白色背景

        ctx.fillStyle="#fff";

        ctx.fillRect(0,0,this.width,this.height);

        _this._draw_( ctx , canvases , canvases.length , 0 ,function(){

            _this.createLogoCanvas( ctx ,function(){

                var oImg = _this.toImage( can );

                oImg.onload = function () {

                    var time = new Date().getTime();
                    //调用
                    _this.export_raw( "报表图"+time+".png" , oImg.src );
                }


            });

        });

    },

    _draw_:function( ctx , canvases ,len , i  , fnEnd ){

        var _this = this;

        var img = _this.toImage( canvases [i] );

        img.onload = function(){

            ctx.drawImage( img ,0 , 0 );

            if( i+1 < len ){

                _this._draw_( ctx , canvases ,len , i + 1 , fnEnd );
            }else{

                if(fnEnd) fnEnd();

            }


        };
    },

    bindButtonEvent: function ( element, type, handler ){

        if( element.addEventListener ) {

            element.addEventListener( type , handler, false);

        } else {

            element.attachEvent('on'+type, handler);
        }
    },
    /**
     *保存图片信息
     */
    saveImageInfo:function ( id , btn_id ){

        this.parent = id;

        this.button = btn_id ;

        this.init();

        this.saveCanvas();
    },
    /**
     * 转换为图片
     * */
    toImage:function( mycanvas ){

        var image = mycanvas.toDataURL("image/png");

        var sImg = new Image();

        sImg.width = this.width;

        sImg.height = this.height;

        sImg.src = image;

        return sImg;

    },
    /**
     *添加自定义事件模拟点击
     **/

    fake_click : function ( obj )
    {
        var ev = document.createEvent("MouseEvents");

        ev.initEvent("click",true,false);

        obj.dispatchEvent( ev );
    },
    /**
     *导出
     **/
    export_raw:function (name, data) {

        var save_link = document.createElement("a");

        save_link.href = data;

        save_link.download = name;

        var body = document.getElementsByTagName( 'body' )[ 0 ];

        this.fake_click( save_link );
    },

    /**
     *获取样式
     */
    getStyle:function (div,name){

        if( div.currentStyle ) {

            return div.currentStyle[ name ];

        } else if( window.getComputedStyle ) {

            return window.getComputedStyle( div , null )[ name ];
        }
    }

};